package leaderus.study;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;

public class MyPersonCaclByMy {
	
	public static void main(String[] args) {
		MyPersonCaclByMy my=new MyPersonCaclByMy();
		String tableFile=GenRandomersonDBFile.path + "//"+ GenRandomersonDBFile.fileName;
		String[][] personArray=my.readTableFile(tableFile);
		Integer age=0;
		String name="";
		int length=personArray.length;
		int[][] count=new int[2][6];
		long startTime=System.currentTimeMillis();
		String[] strings;
		char first;
		for(int i=0;i<length;i++){
			strings=personArray[i];
			age=Integer.valueOf(strings[1]);
			name=strings[0];
			first=name.charAt(0);
			if (age > 5 && age < 12) {
				count[0][0]++;
				if (first=='a' || first=='A')
					count[0][1]++;
				if (first=='b' || first=='B')
					count[0][2]++;
				if (first=='c' || first=='C')
					count[0][3]++;
				if (first=='d' || first=='D')
					count[0][4]++;
				if (first=='e' || first=='E')
					count[0][5]++;
			}
			if (age > 15 && age < 19) {
				count[1][0]++;
				if (first=='a' || first=='A')
					count[1][1]++;
				if (first=='b' || first=='B')
					count[1][2]++;
				if (first=='c' || first=='C')
					count[1][3]++;
				if (first=='d' || first=='D')
					count[1][4]++;
				if (first=='e' || first=='E')
					count[1][5]++;
			}
		}
		System.out.println("查询出统计结果共耗时："+(System.currentTimeMillis()-startTime)+" 毫秒。");
		System.out.println("6-11岁总共：" + count[0][0]
				+ " 个，a开头的：" + count[0][1] + " 个，b开头的："
				+ count[0][2] + " 个，c开头的："
				+ count[0][3] + " 个，d开头的："
				+ count[0][4] + " 个，e开头的："
				+ count[0][5]);
		System.out.println("16-18岁总共：" + count[1][0]
				+ " 个，a开头的：" + count[1][1] + " 个，b开头的："
				+ count[1][2] + " 个，c开头的："
				+ count[1][3] + " 个，d开头的："
				+ count[1][4] + " 个，e开头的："
				+ count[1][5]);
	}
	public String[][] readTableFile(String fileName){
		File personFile = new File(fileName);
		String[][] personArray=new String[1000000][2];
		if (personFile.exists() && personFile.isFile()) {
			try {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(personFile));
				BufferedReader bufferedReader = new BufferedReader(read);
				String infoLine;
				String[] infos;
				int i=0;
				while ((infoLine = bufferedReader.readLine()) != null) {
					infos = infoLine.split(",");
					personArray[i][0]=infos[1];
					personArray[i][1]=infos[2];
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("找不到指定文件！");
		}
		return personArray;
	}
}
